package service;

import dao.RssItemModelDAO;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import readerRSS.RssItemController;
import readerRSS.RssItemModel;

/**
 *
 * @author Tania M
 */
public class ServiceRssItem {

    /**
     * 
     */
    public ServiceRssItem() {
    }

    //conversions entre modèles et DAO
    /**
     * 
     * @param obj
     * @return
     */
    public static RssItemModelDAO modelToDAO(RssItemModel obj) {
        RssItemModelDAO _convert = new RssItemModelDAO();

        _convert.setCategory(obj.getCategory());
        _convert.setDescription(obj.getDescription());
        _convert.setId(obj.getGuid());
        //_convert.setId(Double.NaN);
        _convert.setLink(obj.getLink());
        _convert.setTitle(obj.getTitle());

        return _convert;
    }
    
    /**
     * 
     * @param obj
     * @return
     */
    public static RssItemModelDAO controltoDAO(RssItemController obj) {
        RssItemModelDAO _convert = new RssItemModelDAO();

        _convert.setHasBeenSeen(obj.getHasBeenSeenState().getHasBeenSeen());
        
        _convert.setCategory(obj.getModel().getCategory());
        _convert.setDescription(obj.getModel().getDescription());
        _convert.setId(obj.getModel().getGuid());
        //_convert.setId(Double.NaN);
        _convert.setLink(obj.getModel().getLink());
        _convert.setTitle(obj.getModel().getTitle());

        return _convert;
    }

    /**
     * 
     * @param obj
     * @return
     */
    public static RssItemModel daoToModel(RssItemModelDAO obj) {
        RssItemModel _convert = new RssItemModel();

        _convert.setCategory(obj.getCategory());
        _convert.setDescription(obj.getDescription());
        _convert.setGuid(obj.getId());
        _convert.setLink(obj.getLink());
        _convert.setTitle(obj.getTitle());

        _convert.setViewState(obj.getHasBeenSeen());
        
        return _convert;
    }

    /**
     * 
     * @param objList
     * @return
     */
    public static ArrayList<RssItemModel> daoListToModelArrayList(List<RssItemModelDAO> objList) {
        ArrayList<RssItemModel> _convertedList = new ArrayList<RssItemModel>();

        for (Iterator<RssItemModelDAO> it = objList.iterator(); it.hasNext();) {
            RssItemModelDAO obj = it.next();

            RssItemModel _convert = daoToModel(obj);

            _convertedList.add(_convert);
        }

        return _convertedList;
    }

    /**
     * 
     * @param objList
     * @return
     */
    public static List<RssItemModelDAO> modelArrayListToDaoList(ArrayList<RssItemModel> objList) {
        List<RssItemModelDAO> _convertedList = new ArrayList<RssItemModelDAO>();
        
        for (Iterator<RssItemModel> it = objList.iterator(); it.hasNext();) {
            RssItemModel obj = it.next();
            
            RssItemModelDAO _convert = new RssItemModelDAO();
            _convert.setCategory(obj.getCategory());
            _convert.setDescription(obj.getDescription());
            _convert.setId(obj.getGuid());
            _convert.setLink(obj.getLink());
            _convert.setTitle(obj.getTitle());
            
            _convertedList.add(_convert);
        }

        return _convertedList;
    }
}
